#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        int maxarea = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    maxarea = max(maxarea, dfs(grid, i, j));
                }
            }
        }
        return maxarea;
    }

private:
    int dfs(vector<vector<int>>& grid, int i, int j) {
        int m = grid.size(), n = grid[0].size();
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
            return 0;
        }
        grid[i][j] = 0;
        int total = 1 + dfs(grid, i + 1, j) + dfs(grid, i - 1, j) +
            dfs(grid, i, j + 1) + dfs(grid, i, j - 1);
        return total;
    }
};